package actions;

import java.util.ArrayList;
import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.UserManagerLocal;
import beans.UserManager;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;
import entities.User;

public class LoadUserAdminAction extends ActionSupport {
	
	private User user = new User();

	@EJB
	private UserManagerLocal userMgr = new UserManager();
	private ArrayList<String> userTypes = new ArrayList<String>();
	
	public String execute() {
		
		int userId = 0;
		userTypes.add(User.USER);
		userTypes.add(User.ADMIN);

		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");

		if (id == null)
			return "loginerror";

		if (ServletActionContext.getRequest().getParameter("userId") != null) {
			String userIdStr = ServletActionContext.getRequest().getParameter("userId");
			userIdStr = userIdStr.trim();
			userId = Integer.parseInt(userIdStr);
			user = userMgr.getUser(userId);
			return "success";
		}
		else {
			return "error";
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserManagerLocal getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserManagerLocal userMgr) {
		this.userMgr = userMgr;
	}

	public ArrayList<String> getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(ArrayList<String> userTypes) {
		this.userTypes = userTypes;
	}

}
